import { Injectable } from '@angular/core';
import { Observable, fromEvent, debounceTime, map, distinctUntilChanged } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ScreenService {
// 实时屏幕宽度Observable
  screenWidth$: Observable<number> = fromEvent(window, 'resize').pipe(
    debounceTime(100), // 防抖：减少频繁触发
    map(() => window.innerWidth),
    distinctUntilChanged() // 仅当宽度变化时触发
  );
 
  // 获取初始宽度
  getInitialWidth(): number {
    return window.innerWidth;
  }
}
